package jwei.apps;

import java.util.List;

import jwei.apps.adapters.ReviewBaseAdapter;
import jwei.apps.objects.Review;
import jwei.apps.zappos.ZapposGetReviews;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;

public class ProductReviewsActivity extends ListActivity {

    private ProgressDialog pd;

    private ReviewBaseAdapter rBA;

    private long productId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_review_layout);

        Intent i = getIntent();
        productId = i.getLongExtra("productId", 0);

        new GetReviewsTask().execute(productId);
    }

    private class GetReviewsTask extends AsyncTask<Long, String, List<Review>> {

        @Override
        protected void onPreExecute() {
            pd = ProgressDialog.show(ProductReviewsActivity.this, "Getting Reviews",
                    "Please wait while we get reviews for this product...", true, false);
        }

        // param[0] is product id
        protected List<Review> doInBackground(Long... param) {
            long id = param[0];
            List<Review> ret = ZapposGetReviews.getReviews(id);
            return ret;
        }

        protected void onProgressUpdate(String... progress) {
            pd.setMessage(progress[0]);
        }

        protected void onPostExecute(List<Review> reviews) {
            if (reviews.size() > 0) {
                // create base adapter
                rBA = new ReviewBaseAdapter(ProductReviewsActivity.this, reviews);

                // set as current list
                setListAdapter(rBA);
            } else {
                Toast t = Toast.makeText(ProductReviewsActivity.this,
                        "Could not retrieve reviews for this product - check back with Zappos in a bit!",
                        Toast.LENGTH_SHORT);
                t.show();
            }
            if (pd.isShowing()) {
                pd.dismiss();
            }
        }
    }

}
